#include "ComponentHandler.h"
#include <vector>
#include "Singleton.h"

template<typename T>
class TStaticComponentAllocator
{
public:
    T* alloc(Entity* entity)
    {
        T* comp = new T();
        comp->owner = entity;
        components.push_back(comp);
        ptr->onstart();
        return comp;
    }

    void release(T* ptr)
    {
        for(auto it = components.begin(); it != components.end();)
        {
            if(*it == ptr)
            {
                *it = components.erase(it);
                ptr->onremove();
                delete ptr;
            }
            else
                ++it;
        }
    }

private:
    std::vector<T*> components;
};


#define DECLARE_STATIC_COMPONENT(component) \
    class component##Allocator  \
    : public TStaticComponentAllocator<component>\
    , public Singleton<component##Allocator>\
{\
};\
class component##Handler \
: public ComponentHandler<component,component##Allocator>\
{\
public:\
    component##Handler(Entity* owner)\
        :ComponentHandler<component,component##Allocator>(owner)\
    {\
    }\
};